home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Treiber / Misc / Mroocheck / FreeWheel / Source / WheelMouse.c < prev    next >
C/C++ Source or Header  |  2000-04-20  |  5KB  |  210 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #include <exec/types.h>
  6. #include <exec/io.h>
  7.  
  8. #include <devices/input.h>
  9. #include <intuition/newmouse.h>
  10.  
  11. #include <clib/exec_protos.h>
  12.  
  13. #include "WheelMouse.h"
  14.  
  15. char *WindowModeNames[]={"Window under pointer",
  16.                          "Active window",NULL };
  17.  
  18. char *ClickModeNames[]={"Ignore",
  19.                          "Shift",
  20.                          "Shift + Left Button",
  21.                          "Toggle Left Button",
  22.                          "Movement -> Scroll",
  23.                          "Move/Scroll Toggle",
  24.                          "Cycle screens",NULL};
  25.  
  26. char *ClickRollModeNames[]={"Ignore",
  27.                            "Depth Arrange Windows",
  28.                            "Horizontal Scroll",NULL};
  29.  
  30. char *DoubleClickModeNames[]={"Ignore",
  31.                               "Cycle Screens",
  32.                               "Swap Axis",NULL};
  33.  
  34. void *InputBase;
  35.  
  36. BOOL WheelMouse_Handle(struct WheelMouseContext *wmc,unsigned long signals);
  37. void WheelMouse_Dispose(struct WheelMouseContext *wmc);
  38.  
  39.  
  40. void WheelMouse_Dispose(struct WheelMouseContext *wmc)
  41. {
  42.   if(wmc)
  43.   {
  44.     if(wmc->IOReq)
  45.     {
  46.       CloseDevice((struct IORequest *)wmc->IOReq);
  47.       DeleteIORequest((struct IORequest *)wmc->IOReq);
  48.       wmc->IOReq=NULL;
  49.     }
  50.     if(wmc->IOPort)
  51.       DeleteMsgPort(wmc->IOPort);
  52.     wmc->IOPort=NULL;
  53.  
  54.     if(wmc->ReplyPort)
  55.       DeleteMsgPort(wmc->ReplyPort);
  56.  
  57.     if(wmc->SigBit>-1)
  58.       FreeSignal(wmc->SigBit);
  59.     free(wmc);
  60.   }
  61. }
  62.  
  63.  
  64. struct WheelMouseContext *WheelMouse_Create()
  65. {
  66.   struct WheelMouseContext *wmc;
  67.   if(!(wmc=malloc(sizeof(struct WheelMouseContext))))
  68.     return(NULL);
  69.   memset(wmc,0,sizeof(struct WheelMouseContext));
  70.  
  71.   wmc->Dispose=WheelMouse_Dispose;
  72.   wmc->Handle=WheelMouse_Handle;
  73.  
  74.   wmc->WindowModeNames=WindowModeNames;
  75.   wmc->MidButton.ClickModeNames=ClickModeNames;
  76.   wmc->MidButton.ClickRollModeNames=ClickRollModeNames;
  77.   wmc->MidButton.DoubleClickModeNames=DoubleClickModeNames;
  78.   wmc->FourthButton.ClickModeNames=ClickModeNames;
  79.   wmc->FourthButton.ClickRollModeNames=ClickRollModeNames;
  80.   wmc->FourthButton.DoubleClickModeNames=DoubleClickModeNames;
  81.   wmc->MouseSpeedX=wmc->MouseSpeedY=100;
  82.   wmc->ClickToFront=FALSE;
  83.  
  84.   wmc->MainTask=FindTask(NULL);
  85.   if((wmc->SigBit=AllocSignal(-1))==-1)
  86.   {
  87.     wmc->Dispose(wmc);
  88.     return(NULL);
  89.   }
  90.   wmc->Signals=1<<wmc->SigBit;
  91.  
  92.   if(!(wmc->ReplyPort=CreateMsgPort()))
  93.   {
  94.     wmc->Dispose(wmc);
  95.     return(NULL);
  96.   }
  97.  
  98.   if(!(wmc->IOPort=CreateMsgPort()))
  99.   {
  100.     wmc->Dispose(wmc);
  101.     return(NULL);
  102.   }
  103.  
  104.   if(!(wmc->IOReq=(struct IOStdReq *)CreateIORequest(wmc->IOPort,sizeof(struct IOStdReq))))
  105.   {
  106.     wmc->Dispose(wmc);
  107.     return(NULL);
  108.   }
  109.  
  110.   if(OpenDevice("input.device",0,(struct IORequest *)wmc->IOReq,0))
  111.   {
  112.     DeleteIORequest((struct IORequest *)wmc->IOReq);
  113.     wmc->IOReq=NULL;
  114.     wmc->Dispose(wmc);
  115.   }
  116.   InputBase=wmc->IOReq->io_Device;
  117.  
  118.   wmc->IOReq->io_Command=IND_WRITEEVENT;
  119.   wmc->IOReq->io_Length=sizeof(struct InputEvent);
  120.   wmc->IOReq->io_Data=&wmc->Event;
  121.  
  122.   return(wmc);
  123. }
  124.  
  125.  
  126. void AddIntAtomic(int *a,int v)
  127. {
  128.   *a+=v;  /* Make sure your compiler generates a single instruction
  129.              for this operation, i.e. add.l d0,(a0), and not
  130.              move.l (a0),d1   add.l d0,d1   move.l d1,(a0) */
  131. }
  132.  
  133.  
  134. BOOL WheelMouse_Handle(struct WheelMouseContext *wmc,unsigned long signals)
  135. {
  136.   int distance;
  137.   if(!(signals&wmc->Signals))
  138.     return(TRUE);
  139.  
  140.   while(wmc->ScrollX||wmc->ScrollY||wmc->FakeX||wmc->FakeY)
  141.   {
  142.     if(distance=wmc->ScrollX)
  143.     {
  144.       distance=DoScroll(wmc,FREEHORIZ,distance);
  145.       AddIntAtomic(&wmc->ScrollX,-distance);
  146.     }
  147.  
  148.     if(distance=wmc->ScrollY)
  149.     {
  150.       distance=DoScroll(wmc,FREEHORIZ|FREEVERT,distance);
  151.       AddIntAtomic(&wmc->ScrollY,-distance);
  152.     }
  153.  
  154.     if(wmc->FakeX<0)
  155.     {
  156.       AddIntAtomic(&wmc->FakeX,1);
  157.       wmc->Event.ie_Class=IECLASS_RAWKEY;
  158.       wmc->Event.ie_Code=NM_WHEEL_LEFT;
  159.       wmc->Event.ie_Qualifier=PeekQualifier();
  160.       DoIO((struct IORequest *)wmc->IOReq);
  161.       wmc->Event.ie_Class=IECLASS_NEWMOUSE;
  162.       wmc->Event.ie_Code=NM_WHEEL_LEFT;
  163.       wmc->Event.ie_Qualifier=PeekQualifier();
  164.       DoIO((struct IORequest *)wmc->IOReq);
  165.     }
  166.  
  167.     if(wmc->FakeX>0)
  168.     {
  169.       AddIntAtomic(&wmc->FakeX,-1);
  170.       wmc->Event.ie_Class=IECLASS_RAWKEY;
  171.       wmc->Event.ie_Code=NM_WHEEL_RIGHT;
  172.       wmc->Event.ie_Qualifier=PeekQualifier();
  173.       DoIO((struct IORequest *)wmc->IOReq);
  174.       wmc->Event.ie_Class=IECLASS_NEWMOUSE;
  175.       wmc->Event.ie_Code=NM_WHEEL_RIGHT;
  176.       wmc->Event.ie_Qualifier=PeekQualifier();
  177.       DoIO((struct IORequest *)wmc->IOReq);
  178.     }
  179.  
  180.     if(wmc->FakeY<0)
  181.     {
  182.       AddIntAtomic(&wmc->FakeY,1);
  183.       wmc->Event.ie_Class=IECLASS_RAWKEY;
  184.       wmc->Event.ie_Code=NM_WHEEL_UP;
  185.       wmc->Event.ie_Qualifier=PeekQualifier();
  186.       DoIO((struct IORequest *)wmc->IOReq);
  187.       wmc->Event.ie_Class=IECLASS_NEWMOUSE;
  188.       wmc->Event.ie_Code=NM_WHEEL_UP;
  189.       wmc->Event.ie_Qualifier=PeekQualifier();
  190.       DoIO((struct IORequest *)wmc->IOReq);
  191.     }
  192.  
  193.     if(wmc->FakeY>0)
  194.     {
  195.       AddIntAtomic(&wmc->FakeY,-1);
  196.       wmc->Event.ie_Class=IECLASS_RAWKEY;
  197.       wmc->Event.ie_Code=NM_WHEEL_DOWN;
  198.       wmc->Event.ie_Qualifier=PeekQualifier();
  199.       DoIO((struct IORequest *)wmc->IOReq);
  200.       wmc->Event.ie_Class=IECLASS_NEWMOUSE;
  201.       wmc->Event.ie_Code=NM_WHEEL_DOWN;
  202.       wmc->Event.ie_Qualifier=PeekQualifier();
  203.       DoIO((struct IORequest *)wmc->IOReq);
  204.     }
  205.  
  206.   }
  207.   return(TRUE);
  208. }
  209.  
  210.